package com.supermap.dataservice.mysql.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.mysql.config.TargetDataSource;
import com.supermap.dataservice.mysql.dao.DataMapper;
import com.supermap.dataservice.mysql.service.DataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author xiaolu
 */
@Service
public class DataServiceImpl implements DataService {
    private static final Logger logger = LoggerFactory.getLogger(DataServiceImpl.class);
    private static final int SQL_ERROR_CODE = 400;
    private static final int EXP_ERROR_CODE = 404;

    private static final String SELECT = "SELECT";
    private static final String UPDATE = "UPDATE";
    private static final String INSERT = "INSERT";
    private static final String DELETE = "DELETE";

    @Resource
    private DataMapper dataMapper;

    @Override
    @TargetDataSource
    public JSONObject selectData(String sql) {
        return executeMethod(sql, SELECT);
    }

    @Override
    @TargetDataSource
    public JSONObject updateData(String sql) {
        return executeMethod(sql, UPDATE);
    }

    @Override
    @TargetDataSource
    public JSONObject insertData(String sql) {
        return executeMethod(sql, INSERT);
    }

    @Override
    @TargetDataSource
    public JSONObject deleteData(String sql) {
        return executeMethod(sql, DELETE);
    }

    private JSONObject executeMethod(String sql, String sqlType) {
        logger.info("execute:" + sql + sql.trim().toUpperCase().startsWith(sqlType));
        if (sql.trim().toUpperCase().startsWith(sqlType)) {
            try {
                Object data = null;
                switch (sqlType) {
                    case SELECT:
                        data = dataMapper.select(sql);
                        break;
                    case UPDATE:
                        data = dataMapper.update(sql);
                        break;
                    case INSERT:
                        data = dataMapper.insert(sql);
                        break;
                    case DELETE:
                        data = dataMapper.delete(sql);
                        break;
                    default:
                }
                return successMsg(sqlType, data);
            } catch (Exception e) {
                return errorMsg(sqlType, EXP_ERROR_CODE);
            }

        }
        return errorMsg(sqlType, SQL_ERROR_CODE);
    }

    /**
     * @param sqlType   SQL类型
     * @param errorCode 错误代码
     * @return 查询结果
     */
    private JSONObject errorMsg(String sqlType, int errorCode) {
        JSONObject msg = new JSONObject();
        msg.put("code", errorCode);
        if (errorCode == SQL_ERROR_CODE) {
            msg.put("msg", "NOT A " + sqlType.toUpperCase() + " SQL");
        } else {
            msg.put("msg", sqlType.toUpperCase() + " DATA GET AN EXCEPTION");
        }
        return msg;
    }

    /**
     * @param sqlType SQL类型
     * @param data    数据
     * @return 查询结果
     */
    private JSONObject successMsg(String sqlType, Object data) {
        JSONObject msg = new JSONObject();
        msg.put("code", 200);
        msg.put("msg", sqlType.toUpperCase() + " SUCCESS");
        msg.put("data", data);
        return msg;
    }
}
